package com.ruoyi.web.controller.report;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.vo.ValidateSqlVO;
import com.ruoyi.web.service.DatabaseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DataReport;
import com.ruoyi.system.service.IDataReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2025-04-17
 */
@RestController
@RequestMapping("/report")
public class DataReportController extends BaseController
{
    @Autowired
    private IDataReportService dataReportService;

    @Autowired
    private DatabaseService databaseService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataReport dataReport)
    {
        startPage();
        List<DataReport> list = dataReportService.selectDataReportList(dataReport);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataReport dataReport)
    {
        List<DataReport> list = dataReportService.selectDataReportList(dataReport);
        ExcelUtil<DataReport> util = new ExcelUtil<DataReport>(DataReport.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:report:query')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam(value = "reportId") Long reportId)
    {
        return success(dataReportService.selectDataReportByReportId(reportId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:report:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataReport dataReport)
    {
        return toAjax(dataReportService.insertDataReport(dataReport));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataReport dataReport)
    {
        return toAjax(dataReportService.updateDataReport(dataReport));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:report:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds)
    {
        return toAjax(dataReportService.deleteDataReportByReportIds(reportIds));
    }

    @GetMapping(value="/dblist")
    public AjaxResult dbList() {
        List<String> allDatabases = databaseService.getAllDatabases();
        for (String string : allDatabases) {
            logger.debug("db info:"+string);
        }
        return success(allDatabases);
    }

    @PreAuthorize("@ss.hasPermi('system:report:tablelist')")
    @GetMapping(value="/tablelist")
    public AjaxResult tableList(@RequestParam(value = "dbName") String dbName) {
        List<Map<String, String>> allTables = databaseService.listAllTables(dbName);
        return success(allTables);
    }

    @PreAuthorize("@ss.hasPermi('system:report:columnlist')")
    @GetMapping(value="/columnlist")
    public AjaxResult columnList(@RequestParam(value = "dbName") String dbName, @RequestParam(value = "tableName") String tableName) {
        if (StringUtils.isEmpty(dbName) || StringUtils.isEmpty(tableName)) {
            return success("传入参数错误");
        }
        List<Map<String, String>> columnList = databaseService.listColumns(dbName, tableName);
        return success(columnList);
    }

    @PreAuthorize("@ss.hasPermi('system:report:validateSql')")
    @PostMapping(value="/validateSql")
    public AjaxResult validateSql(@RequestBody ValidateSqlVO sqlVO) {
        String sql = sqlVO.getSql();
        if (StringUtils.isEmpty(sql)) {
            return success("传入参数错误");
        }
        boolean validateSql = databaseService.validateSql(sql);
        return success(validateSql);
    }

    @PreAuthorize("@ss.hasPermi('system:report:generateChart')")
    @PostMapping(value="/generateChart")
    public AjaxResult generateChart(@RequestBody ValidateSqlVO sqlVO) {
        String sql = sqlVO.getSql();
        if (StringUtils.isEmpty(sql)) {
            return success("传入参数错误");
        }
        List<Map<String, Object>> list = databaseService.generateChart(sql);
        return success(list);
    }
}

